package com.tensquare.sms.listener;

import com.aliyuncs.exceptions.ClientException;
import com.tensquare.sms.util.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 监听器
 * 从RabbitMQ中获取短信内容(验证码),然后将短信内容发送给阿里云
 */
@Component
@RabbitListener(queues = {"sms"})   //声明从哪些队列里接收消息
public class SmsListener {

    @Value("${aliyun.sms.template_code}")
    private String template_code;
    @Value("${aliyun.sms.sign_name}")
    private String sign_name;
    @Autowired
    private SmsUtil smsUtil;

    //接收消息的处理方法
    @RabbitHandler
    public void reciveMsg(Map<String,String> map) { //这里参数类型,发送的什么类型,就用什么类型接收,例如发送的是User类型,就用User user参数接收
        System.out.println("手机号::"+map.get("phoneNumber"));
        System.out.println("验证码::"+map.get("checkCode"));
        //利用阿里云发短信
        try {
            smsUtil.sendSms(map.get("phoneNumber"),template_code,sign_name,"{\"code\":\""+map.get("checkCode")+"\"}");
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }
}
